/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __K_EVENT_H__
#define __K_EVENT_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdint.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_list.h>
#include <x_module.h>

#include <k_errno.h>
#include <k_tick.h>
#include <k_obj.h>
/* includes (local) ----------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

#define EVENT_WAIT_ALL 1
#define EVENT_WAIT_ANY 2
#define EVENT_WAIT_CLR 4

#define ERRNO_EVENT_ILLEGAL_ID        ERRNO_JOIN(MID_EVENT, 1)
#define ERRNO_EVENT_TIMEOUT           ERRNO_JOIN(MID_EVENT, 2)
#define ERRNO_EVENT_DELETED           ERRNO_JOIN(MID_EVENT, 3)
#define ERRNO_EVENT_UNAVAILABLE       ERRNO_JOIN(MID_EVENT, 4)
#define ERRNO_EVENT_ILLEGAL_OPERATION ERRNO_JOIN(MID_EVENT, 5)

/* typedefs ------------------------------------------------------------------*/

typedef struct event {
    obj_t obj;
    uint32_t event_set;
    dlist_t pend_q;
} event_t, *event_id;

/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

extern int event_init(event_id event);
extern event_id event_create(void);
extern event_id event_open(const char *name, int oflag, ...);
extern int event_destroy(event_id event);
extern int event_timedrecv(event_id event, uint32_t wanted, uint32_t option, unsigned int timeout,
                           uint32_t *recved);
extern int event_recv(event_id event, uint32_t wanted, uint32_t option, uint32_t *recved);
extern int event_tryrecv(event_id event, uint32_t wanted, uint32_t option, uint32_t *recved);
extern int event_send(event_id event, uint32_t events);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __K_EVENT_H__ */
